// index.js
// 获取应用实例
import {categoryList,adPage } from '../../common/api/index'
import {
  commodityList,
  knowPage
} from '../../common/api/shop.js'
const app = getApp()

Page({
  data: {
    content: [],
    navBarHeight: getApp().globalData.navBarHeight,
    statusBarHeight:getApp().globalData.statusBarHeight,
    tabData:[],
    activeIndex:0,
    goodsData:[13,3,3,3,3,3,],
    commodityArr: [],
    page: 1, // 页码
    goodsName: '', // 搜索名字
    bottomLoading: true,
    refresherTriggered: false,
    adList: [],
    newlyAnnouncedLsit: [],
  },
 
  onLoad() {
    this.getAdPage();
    this.getCategory();
  
    
  },
  onShareAppMessage(e){
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../images/shareImg.jpg",
      path: "/pages/index/index?id=" + user.id
    }
  },
  // 页面下路刷新
  onPullDownRefresh(){
    this.bindreFresherrefresh();
    this.setData({
      newlyAnnouncedLsit: []
    });
    this.newlyAnnounced();
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000);
  },
  // 最新揭晓
  newlyAnnounced:async function(){
    let data = {
      categoryId : this.data.tabData[this.data.activeIndex].id,
      type: '',
      page: this.data.page,
      size: 10,
      goodsName: this.data.goodsName,
      isOnSale: '1',
      isBuy: '0',
      isNew: 1
    };
    let res = await knowPage(data);
    console.log(res.list, '最新揭晓商品');
    
    let arr = res.list.map(e =>{
      let goodsName = e.goodsName.length> 12 ? e.goodsName.substring(0,12)+"...": e.goodsName;
      return e.second ? `正在揭晓${goodsName}，快来看看!`:`已经揭晓${goodsName}，快来围观！`;
    });
    console.log(arr , "zxcx")
    this.setData({
      newlyAnnouncedLsit: res.list,
      content: arr
    })
  },
  // 商品倒计时借宿
  finish(e){
    let row = e.currentTarget.dataset.item;
    let newlyAnnouncedLsit = JSON.parse(JSON.stringify(this.data.newlyAnnouncedLsit))
    newlyAnnouncedLsit.forEach((e, index) =>{
      if(row.id === e.id){
        e.end = true;

        let keys = "newlyAnnouncedLsit[" + index + "]";

        this.setData({
          [keys]: e
        });
      }
    });
    // this.setData({
    //   newlyAnnouncedLsit: newlyAnnouncedLsit
    // });

  },
  getCategory: async function () {
    // const vm = this;
    // utils.get("category/list").then((res)=>{
    //   vm.setData({
    //     categoryData:res
    //   })
    // })
    let res = await categoryList({});
    this.setData({
      tabData: res
    });
    console.log('事件名,getCategory')
    // 请求商品；
    this.getShop();
    this.newlyAnnounced();
  },
  sclick: function(e){
    // console.log("点击不了",e)
    this.morePage();
  },
    /**
   * 搜索
   */
  getSeacrh: function(){
    this.bindreFresherrefresh()
   },
   /**
   * 获取商品列表
   */
  getShop: async function () {
    let data = {
      categoryId : this.data.tabData[this.data.activeIndex].id,
      type: '',
      page: this.data.page,
      size: 10,
      goodsName: this.data.goodsName,
      isOnSale: '1',
      isBuy: '0',
      isGold: 0
    };
    let res = await commodityList(data);
    // console.log(res.list, '商品列表')
    // 测试分页
    // let arr2 = [];
    // for(let i=0;i<10;i++){
    //   arr2.push(...res.list)
    // };
    // let arr = arr2 || [];
    let arr = res.list || [];
      if(arr.length < data.size){
        this.setData({
          bottomLoading: false
        })
      }
    let a = [...this.data.commodityArr, ...arr];
    this.setData({
      commodityArr: a,
      refresherTriggered: false
    });
  },
  // 
  bindBottomLoading(){
    if(!this.data.bottomLoading){
      console.log('已经没有了')
      return
    }
    this.data.page+=1;
    console.log('事件名,bindBottomLoading')
    this.getShop()
  },
  bindreFresherrefresh(){
    this.data.page = 1;
    this.setData({
      bottomLoading: true,
      commodityArr: []
    });
    console.log('事件名,bindreFresherrefresh')
    this.getShop();
  },
  inputChange: function(e){
    this.data.goodsName = e.detail.value;
  },
  // 切换
  swiperBindchange(e){
      console.log(e.detail.current, '切换')
      this.setData({
        activeIndex: e.detail.current
      });
      this.bindreFresherrefresh();
  },
  changeTab:function(e){
    this.setData({
      activeIndex:e.currentTarget.dataset.index
    });
    // this.bindreFresherrefresh()
  },
  /**
   * 请求广告
   * */ 
   async getAdPage(){
    let res = await adPage({size: 10, page: 1,title: ''})
    this.setData({
      adList: res.list
    })
  },
  go: function (e) {
    wx.navigateTo({
      url: '/pages/allShop/shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id +'&notCart=true',
      // url: '../shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id,
    })
  },
  go2: function (e) {
    wx.navigateTo({
      url: '/pages/allShop/shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id + '&type=3&notCart=true',
      // url: '../shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id,
    })
  },
  morePage(){
    // wx.switchTab({
    //   url: '/pages/announce/announce',
    // })
    wx.navigateTo({
      url: '/pages/announce2/announce2',
    })
  },

  // 预览
  preview(e){
    console.log()
    let item = e.currentTarget.dataset.item;
    // console.log(item.urlSrc)
    wx.previewImage({
      urls: [item.urlSrc],
    })
  },
})
